
;
(function($, $$) {
    $.fn.slide = function(options) {
        var self = this, args = Array.prototype.slice.call(arguments);
        args.push($$.Slide);
        var result = $$.createWidgetByJQuery.apply(self, args);
        if (result.jqObj !== undefined && options !== "widget") {
            return self;
        }
        return result;
    };

    $$.Slide = function(self, options) {
        if (!(this instanceof $$.Slide)) {
            return new $$.Slide(self, options);
        }
        var defaults = {

            mode : "horizontal",

            slideClass : $$.defaultGradientBG + " uiCornerAll " + $$.defaultBorder,

            sliderCrossClass : $$.hoverGradientBG,

            slideClassVerticalMode : "uiCss3BlackVerticalGradientBG uiBlackBorder uiCornerAll",

            sliderCrossClassVerticalMode : "uiCss3BlueVerticalGradientBG",

            sliderClass : "uiBlackBorder uiDefaultSliderBG uiCornerAll",

            sliderLength : 8,

            sliderTop : -5,

            sliderLeft : -5,

            noUserEvent : false,

            userImageSlider : false,

            min : 0,

            max : 100,

            step : 0.2,

            animation : true,

            click : null,

            slide : null,


            slideLength : 0,

            slideValue : 0,

            slideMax : 0,

            slideBegin : 0,

            panelHTML : '<div class="uiPanel"></div>',

            sliderCrossHTML : '<div class="uiSliderCross"></div>',

            sliderHTML : '<div class="uiSlider"></div>',

            slideDrag : false

        };
        var opts = $.extend(defaults, options);
        $$.Slide.prototype.superClass.call(this, self, opts);
        this.init();
    }
    $$.inherit($$.Slide, $$.Widget, {

        init : function() {
            var slideObj = this;
            slideObj.createWidget();
            initSlide(slideObj.jqObj, slideObj.opts);
            return slideObj;
        },

        val : function(value, animate) {
            var slideObj = this, self = slideObj.jqObj, opts = slideObj.opts;
            if (value === undefined) {
                return opts.slideValue;
            }
            if (animate === undefined) {
                animate = true;
            }
            if (!opts.slideDrag) {
                setSlide.call(self, value, animate, true);
            }
            return slideObj;
        },

        beforeDestroy : function() {
            var slideObj = this, self = slideObj.jqObj, opts = slideObj.opts;
            $(document).unbind("." + opts.widgetKey);
        }
    });

    function initSlide(self, opts) {


        if (opts.mode === "vertical") {
            opts.slideClass = opts.slideClassVerticalMode;
            opts.sliderCrossClass = opts.sliderCrossClassVerticalMode;
        }
        var slider = $(opts.sliderHTML);
        var sliderCross = $(opts.sliderCrossHTML).addClass(opts.sliderCrossClass);
        if (opts.userImageSlider) {
            slider.addClass("uiImageSlider uiIcon uiCicleIcon");
        }
        else {
            slider.addClass(opts.sliderClass);
        }
        self.addClass("uiSlide uiWidget uiNoSelectText " + opts.slideClass).append($(opts.panelHTML).append(sliderCross).append(slider));
        if (opts.userImageSlider) {
            opts.sliderLength = slider.width();
        }
        if (opts.mode === "vertical") {
            opts.slideBegin = opts.sliderTop = self.offset().top;
            slider.css("left", opts.sliderLeft - 1);
            sliderCross.width("100%");
            if (!opts.userImageSlider) {
                slider.width(self.width() - 2 * opts.sliderLeft).height(opts.sliderLength);
            }
            else {
                slider.css("top", -(opts.sliderLength >> 2));
            }
            opts.slideLength = self.height();
        }
        else {
            opts.slideBegin = opts.sliderLeft = self.offset().left;
            slider.css("top", opts.sliderTop - 1);
            sliderCross.height("100%");
            if (!opts.userImageSlider) {
                slider.height(self.height() - 2 * opts.sliderTop).width(opts.sliderLength);
            }
            else {
                slider.css("left", -(opts.sliderLength >> 2));
            }
            opts.slideLength = self.width();
        }
        opts.slideMax = opts.slideLength;
        if (!opts.userImageSlider) {
            opts.slideMax -= (opts.sliderLength - 2);
        }


        if (!opts.noUserEvent) {
            initEvent(self, opts);
        }
        if (opts.slideValue !== 0) {
            setSlide(self, opts, opts.slideValue);
        }
        if (opts.disabled) {
        }
        return self;
    }


    function initEvent(self, opts) {

        $("> .uiPanel", self).bind("click.uiSlide", function(e) {
            if (opts.disabled || $(e.target).hasClass("uiSlider")) {
                return false;
            }
            if ($.isFunction(opts.click)) {
                if (opts.click.call(self[0], $(this), e) === false) {
                    return false;
                }
            }
            var beginValue;
            if (opts.mode === "vertical") {
                beginValue = e.clientY + $(document).scrollTop() + self.parent().scrollTop();
            }
            else {
                beginValue = e.clientX + $(document).scrollLeft() + self.parent().scrollLeft();
            }
            var percent = (beginValue - opts.slideBegin - (opts.sliderLength >> 1)) / opts.slideMax;
            setSlide(self, opts, Math.floor(percent * (opts.max - opts.min) + opts.min), opts.animation);
        })
            .bind("mousewheel.uiSlide", function(e, delta) {
                if (opts.disabled) {
                    return false;
                }
                var positionStr = "left";
                if (opts.mode === "vertical") {
                    positionStr = "top";
                }
                var percent = parseInt($("> .uiSlider", this).css(positionStr)) / opts.slideMax + opts.step * (-delta / 2);
                console.log(percent);
                setSlide(self, opts, Math.floor(percent * (opts.max - opts.min) + opts.min), false);
                return false;
            });
        var mouseMoveEvent = "mousemove." + opts.widgetKey;
        var mouseUpEvent = "mouseup." + opts.widgetKey;
        $("> .uiPanel > .uiSlider", self).bind("mousedown.uiSlide", function(e) {
            if (opts.disabled) {
                return false;
            }
            opts.slideDrag = true;
        });
        $(document).bind(mouseMoveEvent, function(e) {
            if (opts.slideDrag) {
                var beginValue;
                if (opts.mode === "vertical") {
                    beginValue = e.clientY + $(document).scrollTop() + self.parent().scrollTop();
                }
                else {
                    beginValue = e.clientX + $(document).scrollLeft() + self.parent().scrollLeft();
                }
                var percent = (beginValue - opts.slideBegin - (opts.sliderLength >> 1)) / opts.slideMax;
                setSlide(self, opts, Math.floor(percent * (opts.max - opts.min) + opts.min), false);
            }
        })
            .bind(mouseUpEvent, function() {
                opts.slideDrag = false;
            });
    }


    function setSlide(self, opts, value, animate, jumpToEnd) {
        var obj = $("> .uiPanel > .uiSlider", self);
        value = value || 0;
        value = value > opts.max ? opts.max : (value < 0 ? 0 : value);
        opts.slideValue = value;
        var percent = (value - opts.min) / (opts.max - opts.min);
        value = opts.slideMax * percent;
        value = value > 0 ? (value < opts.slideMax ? value : opts.slideMax) : 0;
        var props;
        if (opts.userImageSlider && value == 0) {
            value = -(opts.sliderLength >> 2);
        }
        var sliderCross = $("> .uiPanel > .uiSliderCross", self);
        var sliderCrossValue = value + 2;
        var sliderCrossProps = null;
        ;
        if (opts.mode === "vertical") {
            props = {"top" : value};
            sliderCrossProps = {"height" : sliderCrossValue};
        }
        else {
            props = {"left" : value};
            sliderCrossProps = {"width" : sliderCrossValue};
        }
        if (animate) {
            obj.stop(true, jumpToEnd).animate(props, opts.animateTime, function() {
                if ($.isFunction(opts.slide)) {
                    opts.slide.call(self[0], obj, opts.slideValue);
                }
            });
            sliderCross.stop(true, jumpToEnd).animate(sliderCrossProps, opts.animateTime);
        }
        else {
            obj.css(props);
            if ($.isFunction(opts.slide)) {
                opts.slide.call(self[0], obj, opts.slideValue);
            }
            sliderCross.css(sliderCrossProps);
        }
        return self;
    }
})(jQuery, BASE);